package com.zhongjian.technologycal.mall.activity.personal.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.adapter.IDCardAdapter;
import com.zhongjian.technologycal.mall.base.BaseActivity;
import com.zhongjian.technologycal.mall.base.BaseContacts;
import com.zhongjian.technologycal.mall.bean.BaseBean;
import com.zhongjian.technologycal.mall.bean.IDCardBean;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.LoadingDialog;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.ToastUitl;
import com.zhongjian.technologycal.mall.utils.UserUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by wei on 2017/12/25.
 * 添加银行卡提现账户
 */

public class AddAlipayAccountIdCardActivity extends BaseActivity {
    @BindView(R.id.tv_main_title)
    TextView tv_main_title;
    @BindView(R.id.alipay_withdraw_et_idcard_name)
    EditText alipay_withdraw_et_idcard_name;
    @BindView(R.id.alipay_withdraw_et_idcard_account)
    EditText alipay_withdraw_et_idcard_account;
    @BindView(R.id.alipay_withdraw_tv_account_idcard)
    TextView alipay_withdraw_tv_account_idcard;
    @BindView(R.id.alipay_withdraw_et_details)
    TextView alipay_withdraw_et_details;
    @BindView(R.id.withdraw_iv_head_card)
    ImageView withdraw_iv_head_card;
    private String idcardlogo;
    private Dialog picChooseDialog = null;
    private IDCardAdapter idCardAdapter;
    private List<IDCardBean> listItems = new ArrayList<>();

    @Override
    protected void beforeInit() {
        super.beforeInit();
//        StatusBarUtil.setColorNoTranslucent(this, getResources().getColor(R.color.border_color));
//        StatusBarUtil.setMiuiStatusBarDarkMode(this, true);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tv_main_title.setText("添加银行卡账号");
        initViewRequest();
    }

    @Override
    public void initPresenter() {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_add_alipay_account_idcard;
    }

    @OnClick(R.id.iv_back_page)
    public void onClickBack() {
        finish();
    }


    @OnClick(R.id.alipay_withdraw_ll_account_idcard)
    public void onClickChoiceIdCard() {
        initshowDialog(AddAlipayAccountIdCardActivity.this);
    }

    /**
     * 弹出对话框
     */
    private void initshowDialog(Context context) {
        picChooseDialog = new Dialog(context, R.style.pic_choose_dialog_progress);
        View view = LayoutInflater.from(context).inflate(
                R.layout.info_choose_idcard_dialog, null);
        final TextView cancel_tv_dialog_commit = view.findViewById(R.id.cancel_tv_dialog_commit);
        final ListView choose_lv_dialog_items = view.findViewById(R.id.choose_lv_dialog_items);
        idCardAdapter = new IDCardAdapter(context, listItems);
        choose_lv_dialog_items.setAdapter(idCardAdapter);

        choose_lv_dialog_items.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                alipay_withdraw_tv_account_idcard.setText("" + listItems.get(position).getData().get(position).getBankname());
                idcardlogo = listItems.get(position).getData().get(position).getLogo();
                Picasso.get().load(BaseContacts.BASE_URL + idcardlogo).into(withdraw_iv_head_card);
                picChooseDialog.dismiss();
            }
        });
        cancel_tv_dialog_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                picChooseDialog.dismiss();
            }
        });
        Window window = picChooseDialog.getWindow();
        window.setContentView(view);
        window.setGravity(Gravity.BOTTOM);
        window.setLayout(android.view.WindowManager.LayoutParams.MATCH_PARENT,
                android.view.WindowManager.LayoutParams.WRAP_CONTENT);
        window.setWindowAnimations(R.style.popupWindowAnimation);
        picChooseDialog.setCanceledOnTouchOutside(true);
        picChooseDialog.show();
    }

    /**
     * 获取银行卡信息
     */
    private void initViewRequest() {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        HttpRetrofitClient.getInstance(getBaseContext()).getUrlRequestgetAllBank(new Subscriber<ResponseBody>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(ResponseBody responseBody) {
                try {
                    Gson gson = new Gson();
                    IDCardBean idCardBean = gson.fromJson(responseBody.string(), IDCardBean.class);
                    if (idCardBean.getError_code() == 0) {
                        List<IDCardBean> forList = new ArrayList<>();
                        for (int i = 0; i < idCardBean.getData().size(); i++) {
                            forList.add(idCardBean);
                        }
                        setData(forList);
                    } else {
                        ToastUitl.showShort(idCardBean.getError_message());
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });

    }

    private void setData(List<IDCardBean> foreshowList) {
        if (listItems.size() > 0) {
            listItems.clear();
        }
        for (IDCardBean vo : foreshowList) {
            listItems.add(vo);
        }
//        idCardAdapter.notifyDataSetChanged();
    }

    /**
     * 添加银行卡
     */
    @OnClick(R.id.alipay_withdraw_tv_card_commit)
    public void onClickAddLLAccount() {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        } else if (TextUtils.isEmpty(alipay_withdraw_et_idcard_name.getText().toString().trim())) {
            ToastUitl.showShort("姓名不能为空");
            return;
        } else if (TextUtils.isEmpty(alipay_withdraw_et_idcard_account.getText().toString().trim())) {
            ToastUitl.showShort("账号不能为空");
            return;
        } else if (TextUtils.isEmpty(alipay_withdraw_tv_account_idcard.getText().toString().trim())) {
            ToastUitl.showShort("请选择银行");
            return;
        } else if (TextUtils.isEmpty(alipay_withdraw_et_details.getText().toString().trim())) {
            ToastUitl.showShort("支行信息不能为空");
            return;
        }
        HttpRetrofitClient.getInstance(getBaseContext())
                .postUrlRequestAddBankAccount(UserUtils.getUserToken(),
                        alipay_withdraw_et_idcard_account.getText().toString(),
                        alipay_withdraw_tv_account_idcard.getText().toString().trim(),
                        alipay_withdraw_et_details.getText().toString().trim(),
                        idcardlogo,
                        alipay_withdraw_et_idcard_name.getText().toString().trim(),
                        new Subscriber<ResponseBody>() {
                            @Override
                            public void onStart() {
                                super.onStart();
                                LoadingDialog.showDialogForLoading(AddAlipayAccountIdCardActivity.this);
                            }

                            @Override
                            public void onCompleted() {
                                LoadingDialog.cancelDialogForLoading();
                            }

                            @Override
                            public void onError(Throwable e) {
                                LoadingDialog.cancelDialogForLoading();
                            }

                            @Override
                            public void onNext(ResponseBody responseBody) {
                                LoadingDialog.cancelDialogForLoading();
                                try {
                                    Gson gson = new Gson();
                                    BaseBean baseBean = gson.fromJson(responseBody.string(), BaseBean.class);
                                    if (baseBean.getError_code() == 0) {
                                        ToastUitl.showShort("添加账号成功");
                                        finish();
                                    } else if (baseBean.getError_code() == 3) {
                                        Intent intent = new Intent();
                                        intent.setAction("com.broadcast.info_out_line");
                                        sendBroadcast(intent);
                                    } else {
                                        ToastUitl.showShort(baseBean.getError_message());
                                    }

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            }
                        });


    }


}
